/**
 * 
 */
package org.bobdawg.app.MALforAndroid;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

/**
 * @author Robert LaFont Jr
 *
 */
public class MALCursorAdapter extends SimpleCursorAdapter {
	
	//private Resources res = getResources();
	
	private final static String LOG_NAME = "MALCursorAdapter";

	/**
	 * @param context  
	 * 			The context where the ListView associated
	 * 			with this SimpleListItemFactory is running 
	 * @param layout  
	 * 			resource identifier of a layout file that 
	 * 			defines the views for this list item. The layout 
	 * 			file should include at least those named views 
	 * 			defined in "to" 
	 * @param c  
	 * 			The database cursor. Can be null if the cursor 
	 * 			is not available yet. 
	 * @param from  
	 * 			A list of column names representing the data 
	 * 			to bind to the UI. Can be null if the cursor is 
	 * 			not available yet. 
	 * @param to  
	 * 			The views that should display column in the 
	 * 			"from" parameter. These should all be TextViews. 
	 * 			The first N views in this list are given the 
	 * 			values of the first N columns in the from 
	 * 			parameter. Can be null if the cursor is not 
	 * 			available yet. 
	 */
	public MALCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
		super(context, layout, c, from, to);
		// TODO Auto-generated constructor stub
	}

	public MALCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
		super(context, layout, c, from, to, flags);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void bindView(View row, Context context, Cursor cursor) {
		super.bindView(row, context, cursor);
		
		File root = Environment.getExternalStorageDirectory();
		File file;
		
		ImageView ivImage = (ImageView) row.findViewById(R.id.image);
		TextView tvScore = (TextView) row.findViewById(R.id.score);
		RatingBar rbScore = (RatingBar) row.findViewById(R.id.scoreRatingBar);
		
		// Manually bind data to views
		try {
			String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
			ivImage.setContentDescription(title);
		} catch (IllegalArgumentException e) {
			Log.e(LOG_NAME,"'title' column does not exist", e);
		}
		try {
			file = new File(root, "Android/data/org.bobdawg.app.MALUpdater/anime/" + String.valueOf(cursor.getColumnIndexOrThrow("id")));
			if (file.exists()) {

				try {
					FileInputStream fis = new FileInputStream(file);
					Bitmap bmImg = BitmapFactory.decodeStream(fis);
					if (bmImg != null) {
						ivImage.setImageBitmap(bmImg);
					} else {
						//startService(i);
					}
				} catch (FileNotFoundException e) {
					Log.e("AnimeDetail", "image error", e);
				}

			} else {
				//startService(i);
			}
		} catch (IllegalArgumentException e) {
			Log.e(LOG_NAME,"'id' column does not exist", e);
			file = new File(root, "Android/data/org.bobdawg.app.MALUpdater/anime/1");
		}
		try {
			int score = cursor.getInt(cursor.getColumnIndexOrThrow("score"));
			tvScore.setText("(" + String.valueOf(score) + ")");
			rbScore.setRating((float) score / (float) 2);
		} catch (IllegalArgumentException e) {
			Log.e(LOG_NAME,"'score' column does not exist", e);
			tvScore.setText("(NA)");
			rbScore.setRating((float) 0);
		}
		
		//Intent i = new Intent((android.app.Activity) this, MALManager.class);
		//i.setAction(MALManager.IMAGE);
		//Bundle b = new Bundle();
		//b.putSerializable("media", ar);
		//i.putExtras(b);

		
	}

}
